package cloud.dandanit.itservice.controller.article;


import cloud.dandanit.itservice.config.limitCurrentConfig.RateLimit;
import cloud.dandanit.itservice.entity.Article;
import cloud.dandanit.itservice.entity.Image;
import cloud.dandanit.itservice.entity.Video;
import cloud.dandanit.itservice.entity.vo.ArticleVo;
import cloud.dandanit.itservice.service.ArticleClassesService;
import cloud.dandanit.itservice.service.ArticleService;
import cloud.dandanit.itservice.service.ImageService;
import cloud.dandanit.itservice.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程简介 前端控制器
 * </p>
 *
 * @author luokai
 * @since 2022-07-05
 */
@SuppressWarnings("DuplicatedCode")
@RestController
@RequestMapping("/itservice/articlefront")
public class ArticleFrontController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleClassesService articleClassesService;
    private Long oldViewNum = 0L;
    private Article oldArticle = null;

    /**
     * 获取前台首页文章方法
     * @return 图片列表
     */
    @GetMapping("getIndexArticle")
    @ApiOperation("获取首页文章接口")
    public R getIndexArticle() {

        List<Article> articleList = articleService.getIndexArticle();
        return R.ok().data("articleList", articleList);
    }


    @ApiOperation(value = "获取文章分页列表")
    @GetMapping("pageArticle/{page}/{limit}")
    public R getPageArticle(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<Article> pageParam = new Page<>(page, limit);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("state", 1)
                    .orderByDesc("gmt_modified")
                    .select("id","title","summary","cls_id", "cls_parent_id","sort","state","difficulty","view_num","gmt_create","gmt_modified");
        articleService.page(pageParam,queryWrapper);


        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }


    /**
     * 分页查看文章
     * @param page 当前页码
     * @param limit 每页记录数
     * @return 分页
     */
    @ApiOperation(value = "获取文章全部信息分页列表")
    @GetMapping("getPageAllFieldArticle/{page}/{limit}")
    public R getPageAllFieldArticle(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<Article> pageParam = new Page<>(page, limit);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", 1)
                    .orderByDesc("gmt_modified");

        articleService.page(pageParam,queryWrapper);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    /**
     * 分页查看文章+指定分类
     * @param page 当前页码
     * @param limit 每页记录数
     * @return 分页
     */
    @ApiOperation(value = "按分类获取文章分页列表")
    @GetMapping("getArtilceListByClassesId/{page}/{limit}/{articleClassesId}")
    public R getArtilceListByClassesId(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "articleClassesId", value = "分类查看", required = true)
            @PathVariable String articleClassesId) {


        Page<Article> pageArticle = new Page<>(page, limit);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();

        // 小于15表示查看按照难度分类
        if (articleClassesId.length() > 14) {
            queryWrapper.eq("cls_id", articleClassesId);
        } else if(!("-1".equals(articleClassesId))) {
            queryWrapper.eq("difficulty", articleClassesId);
        }
        queryWrapper.eq("state", 1)
                .orderByDesc("gmt_modified")
                .select("id","title","summary","cls_id", "cls_parent_id","sort","state","difficulty","view_num","gmt_create","gmt_modified");
        articleService.page(pageArticle,queryWrapper);

        Page<ArticleVo> pageArticleVo = new Page<>(page, limit);

        BeanUtils.copyProperties(pageArticle, pageArticleVo, "records");

        List<Article> records = pageArticle.getRecords();

        List<ArticleVo> articleVoList = records.stream().map((item) -> {
            ArticleVo articleVo = new ArticleVo();
            //将dishDto除了categoryName属性外的属性通过item拷贝过来
            BeanUtils.copyProperties(item, articleVo);

            //获取item属性的categoryId
            String clsParentId = item.getClsParentId();
            String clsId = item.getClsId();

            String clsParentTitle = articleClassesService.getById(clsParentId).getTitle();
            articleVo.setClsParentTitle(clsParentTitle);

            String clsTitle = articleClassesService.getById(clsId).getTitle();
            articleVo.setClsTitle(clsTitle);

            return articleVo;
        }).collect(Collectors.toList());

        pageArticleVo.setRecords(articleVoList);

        return R.ok().data("items", pageArticleVo.getRecords()).data("total", pageArticleVo.getTotal());
    }

    /**
     * 根据id获取文章阅读数
     * @return 文章
     */
    @GetMapping("getViewNumOrAddViewNumById/{articleId}/{flag}")
    @ApiOperation("根据id获取文章接口")
    public R getViewNumOrAddPlayCountById(
            @ApiParam(name = "articleId", value = "文章ID", required = true)
            @PathVariable String articleId,
            @ApiParam(name = "flag", value = "标志(0查询1自增)", required = true)
            @PathVariable Long flag) {

        //查询
        if (flag == 0) {

            Article article = articleService.getArticleById(articleId);
            oldArticle = article;
            oldViewNum = article.getViewNum();
            return R.ok().data("article", article);
        } else {
            //自增
            oldArticle.setViewNum(oldViewNum+1);
            articleService.updateById(oldArticle);
            return R.ok();
        }
    }

}

